﻿package com.java.service.impl;

import java.io.File;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.mapper.UserMapper;
import com.java.po.User;
import com.java.po.UserCustom;
import com.java.po.UserMap;
import com.java.service.UserService;
import com.java.tool.AjaxJson;

/**
 * Title:UserServiceImpl.java
 * Description:
 * @author:sprogrammer
 * @time:2018骞�2鏈�22鏃ヤ笅鍗�7:22:39
 */
public class UserServiceImpl implements UserService {
	@Autowired
	private UserMapper userMapper;
	private	AjaxJson ajaxJson;
	
	public UserServiceImpl(){
		@SuppressWarnings("resource")
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-dao.xml");
		userMapper = (UserMapper) applicationContext.getBean("userMapper");		
		ajaxJson = new AjaxJson();
	}
	
	public void cleanAjaxJson(){
		ajaxJson.setData("");
		ajaxJson.setTotal(0);
		ajaxJson.setMessage("");
	}
	
	public AjaxJson LoginUser(User user) throws Exception {
		cleanAjaxJson();
		if(userMapper.loginUser(user)==null){
			ajaxJson.setMessage("用户不存在");
			return ajaxJson;
		}
		User u = userMapper.loginUser(user);
		if(!u.getPassword().equals(user.getPassword())){
			ajaxJson.setMessage("用户不存在");
			return ajaxJson;
		}
		ajaxJson.setData(u);
		ajaxJson.setMessage("用户登录成功");
		return ajaxJson;
	}

	@Override
	public AjaxJson updateUser(UserCustom userCustom) throws Exception {
		cleanAjaxJson();
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍�
		int flag=0;
		User user = (User)userCustom;
		if(!userCustom.getFileimg().isEmpty()){
			int i =(int)Math.random()*1000;
			Date date = new Date();
			String filename = date.getTime()+""+i;
			String path = "D:/毕业设计/userManagement/graduationProject/WebContent/img/";
			userCustom.getFileimg().transferTo(new File(path + filename+".png"));			
			user.setUserimg(filename);
		}
		
		flag = userMapper.updateUser(user);
		
		if(flag!=1){
			ajaxJson.setMessage("fault");
		}
		else
			ajaxJson.setMessage("success");
		
		return ajaxJson;
	}

	@Override
	public AjaxJson fingAllUser(UserMap usermap) throws Exception {
		cleanAjaxJson();
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍�
		User user = new User();
		int page = usermap.getPage();
		int pageSize = usermap.getPageSize();
		if(usermap.getUser()!=null){
			user = usermap.getUser();
		}
		PageHelper.startPage(page,pageSize);
		List<User> list =userMapper.fingAllUser(user);
		PageInfo<User> pageInfo = new PageInfo<>(list);
		ajaxJson.setData(pageInfo.getList());
		ajaxJson.setTotal((int)pageInfo.getTotal());
		ajaxJson.setMessage("success");
		return ajaxJson;
	}
	
	
	
	@Override
	public AjaxJson findUserByid(User user) throws Exception {
		cleanAjaxJson();
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍�
		User u =userMapper.findUserByid(user);
		ajaxJson.setData(u);
		ajaxJson.setTotal(1);
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	@Transactional
	public AjaxJson delete(User user) throws Exception {
		cleanAjaxJson();
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍�
		userMapper.deleteById(user);
		ajaxJson.setMessage("success");
		return ajaxJson;
	}

	@Override
	public AjaxJson insertUser(UserCustom userCustom) throws Exception {
		cleanAjaxJson();
		// TODO 鑷姩鐢熸垚鐨勬柟娉曞瓨鏍�
		int flag=0;
		User user = (User)userCustom;
		if(!userCustom.getFileimg().isEmpty()){
			int i =(int)Math.random()*1000;
			Date date = new Date();
			String filename = date.getTime()+""+i;
			String path = "D:/毕业设计/userManagement/graduationProject/WebContent/img/";
			userCustom.getFileimg().transferTo(new File(path + File.separator + filename+".png"));			
			user.setUserimg(filename);
		}
		
		flag = userMapper.insertUser(user);
		
		if(flag!=1){
			ajaxJson.setMessage("fault");
		}
		else
			ajaxJson.setMessage("success");
		
		return ajaxJson;
	}
}
